//
//  AddImagesBtnCVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/12/12.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import CooperSwift
import ZLPhotoBrowser

class AddImagesBtnCVCModel: NSObject, CollectionCellLongPressConfigProtocol, BindKeyProtocol {

    func cellCls() -> UICollectionViewCell.Type {
        return AddImagesBtnCVC.self
    }
    
    var model: Any?
    
    required init(_ model: Any?) {
        super.init()
        self.model = model
        self.cellSize = CGSize(Screen.width/3.0 - 0.01, Screen.width/3.0 - 0.01)
    }
    
}

class AddImagesBtnCVC: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    @IBAction func addImageTap(_ sender: Any) {
        
        ZLPhotoConfiguration.default().allowSelectLivePhoto = true
        ZLPhotoConfiguration.default().allowSelectImage = false
        
        let ps = ZLPhotoPreviewSheet()
        
        ps.selectImageBlock = { [weak self] (images, isOriginal) in
            // your code
            guard let cell = self else { return }
            guard let info = cell.info as? AddImagesBtnCVCModel else { return }
            images.forEach { image in
                let imageControl = EditImageControlModel().then {
                    $0.imageSource = image.image
                }
                let cvcModle = CollectionEditImageCVCModel.init(imageControl)
                cvcModle.addTargetKey(info.targetKey, isTarget: true)
                info.postCollectionService(.insertItemLast((0, 0)), info: cvcModle)
            }
        }
        guard let vc = UIApplication.currentVC() else { return }
//        ps.showPreview(animate: true, sender: vc)
        ps.showPhotoLibrary(sender: vc)
    }
    
    override func bindTo(_ info: Any?) {
        self.info = info
        
    }

}
